package com.canbot.u05.activity.presidemode.newcode;

import android.content.Intent;
import android.view.View;
import android.widget.CheckBox;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.canbot.u05.R;
import com.canbot.u05.activity.presidemode.newcode.bean.EDirection;
import com.canbot.u05.activity.presidemode.newcode.bean.MoveBean;
import com.canbot.u05.activity.presidemode.newcode.interfaces.ICircleClick;
import com.canbot.u05.activity.presidemode.newcode.interfaces.IDialogBtnLisenter;
import com.canbot.u05.activity.presidemode.newcode.interfaces.IHeadCommonClick;
import com.canbot.u05.activity.presidemode.newcode.view.HeadCommonView;
import com.canbot.u05.activity.presidemode.newcode.view.NHostMoveSettingsDialog;
import com.canbot.u05.activity.presidemode.newcode.view.UnCircleImageView;
import com.canbot.u05.bean.guide.MapVoiceAndEyes;

import butterknife.BindView;

public class NMoveChoiceActivity extends BaseHostActivity implements ICircleClick {
    @BindView(R.id.headCommonView)
    HeadCommonView headCommonView;
    @BindView(R.id.imageUp)
    UnCircleImageView imageUp;
    @BindView(R.id.imageDown)
    UnCircleImageView imageDown;
    @BindView(R.id.imageLeft)
    UnCircleImageView imageLeft;
    @BindView(R.id.imageRight)
    UnCircleImageView imageRight;
    @BindView(R.id.item_choice_none_move)
    RelativeLayout itemNone;
    @BindView(R.id.none_move_checkbox)
    CheckBox checkBoxNone;
    private MoveBean moveBean;

    @Override
    protected void initEventData() {
        MapVoiceAndEyes.playMapVoiceAndEyes(MapVoiceAndEyes.voiceEyesList.get(23));
        headCommonView.setiHeadCommonClick(pos -> {
            switch (pos) {
                case IHeadCommonClick.LEFT:
                    finish();
                    break;
                case IHeadCommonClick.RIGHT:
                    if (moveBean == null) {
                        if (checkBoxNone.isSelected()) {
                            saveData();
                        } else {
                            Toast.makeText(this, getString(R.string.move_is_null), Toast.LENGTH_SHORT).show();
                        }
                    } else {
                        saveData();
                    }
                    break;
            }
        });
        imageUp.setiCircleClick(this);
        imageDown.setiCircleClick(this);
        imageLeft.setiCircleClick(this);
        imageRight.setiCircleClick(this);
        itemNone.setOnClickListener(v -> {
            imageUp.cancle();
            imageDown.cancle();
            imageRight.cancle();
            imageLeft.cancle();
            checkBoxNone.setSelected(true);
            if (moveBean == null) {
                moveBean = new MoveBean();
            }
            moveBean.orientation = EDirection.NONE;
        });
        initIntent();
    }

    private void initIntent() {
        moveBean = (MoveBean) getIntent().getSerializableExtra(DATA);
        if (moveBean != null) {
            switch (moveBean.orientation) {
                case NONE:
                    checkBoxNone.setSelected(true);
                    break;
                case UP:
                    imageUp.select();
                    break;
                case DOWN:
                    imageDown.select();
                    break;
                case LEFT:
                    imageLeft.select();
                    break;
                case RIGHT:
                    imageRight.select();
                    break;
            }
        } else {
            checkBoxNone.setSelected(true);
            moveBean = new MoveBean();
            moveBean.orientation = EDirection.NONE;
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        MapVoiceAndEyes.stop();
    }

    private void saveData() {
        Intent intent = new Intent();
        intent.putExtra(DATA, moveBean);
        setResult(RESULT_OK, intent);
        finish();
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_nmove_choice;
    }

    @Override
    public void onClick(View v) {
        UnCircleImageView circleImageView = (UnCircleImageView) v;
        EDirection eDirection = circleImageView.geteDirection();
        NHostMoveSettingsDialog nHostMoveSettingsDialog = new NHostMoveSettingsDialog(this);
        nHostMoveSettingsDialog.setiDialogBtnLisenter((IDialogBtnLisenter<MoveBean>) result -> {
            moveBean = new MoveBean();
            moveBean.orientation = eDirection;
            moveBean.speed = result.speed;
            moveBean.distance = result.distance;
            saveData();
        });
        nHostMoveSettingsDialog.show(eDirection);
    }

    @Override
    public void onPress(View view) {

    }

    @Override
    public void onUp() {

    }
}
